﻿//============================================================
// Project: DigitalFactory
// Author: Administrator@DESKTOP-IE3FPT7
// Datetime: 2021-03-23 18:19:38
// Description: TODO >> This is a script Description.
//============================================================

using NodePathFinder.Script;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace NodePathFinder.Editor
{
    public class MapNodeDrawer : UnityEditor.Editor
    {
        /// <summary>
        /// 不够自行扩展
        /// </summary>
        private static Color[] _Precolos;

        [DrawGizmo(GizmoType.Selected)]
        static void DrawGameObjectName(Transform transform, GizmoType gizmoType)
        {
            if (_Precolos == null)
            {
                _Precolos = new Color[30];
                for (int i = 0; i < _Precolos.Length; i++)
                {
                    _Precolos[i] = RandomColor1();
                }
            }
            GUIStyle style = new GUIStyle(GUI.skin.customStyles[0]);
            style.fontSize = 15;
            style.normal.textColor = Color.red;
            NodeTypeMapEntity nodeTypeMapEntity = transform.GetComponent<NodeTypeMapEntity>();
            //避免重复绘制
            if (nodeTypeMapEntity == null)
            {
                nodeTypeMapEntity = transform.GetComponentInParent<NodeTypeMapEntity>();
            }
            if (nodeTypeMapEntity == null)
            {
                return;
            }
            for (int i = 0; i < nodeTypeMapEntity.transform.childCount; i++)
            {
                Vector3 head = nodeTypeMapEntity.transform.GetChild(i).position + Vector3.up * 1;
                Handles.Label(head, nodeTypeMapEntity.transform.GetChild(i).gameObject.name, style);
                Handles.color = Color.red;
                Gizmos.color = Color.red;
                Gizmos.DrawSphere(nodeTypeMapEntity.transform.GetChild(i).position, .2f);
            }

            List<Vector2Int> edgs = nodeTypeMapEntity.Edges;
            for (int i = 0; i < edgs.Count; i++)
            {
                Vector3 s = nodeTypeMapEntity.transform.GetChild(edgs[i].x).position;
                Vector3 e = nodeTypeMapEntity.transform.GetChild(edgs[i].y).position;
                Handles.color = _Precolos[i % 30];
                Handles.DrawAAPolyLine(5, s, e);
                style.fontSize = 9;
                style.normal.textColor = Handles.color;
                Handles.Label((s + e) / 2, Vector3.Distance(s, e).ToString(), style);
            }
        }
        public static Color RandomColor1()
        {
            float r = UnityEngine.Random.Range(0f, 1f);
            float g = UnityEngine.Random.Range(0f, 1f);
            float b = UnityEngine.Random.Range(0f, 1f);
            Color color = new Color(r, g, b);
            return color;
        }

        [MenuItem("GameObject/寻路整理",false,1)]
        public static void ReNameChild() 
        {
            GameObject select = Selection.activeObject as GameObject;
            if (select.GetComponent<NodeTypeMapEntity>() != null)
            {
                for (int i = 0; i < select.transform.childCount; i++)
                {
                    select.transform.GetChild(i).name = i.ToString();
                }
            }
        }
    }
}
